/*****************************************************************************
 * kal_harmonyos_timer.c
 *
 * Copyright (C) 2019 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/

#include "plum_api.h"
#include "kal/source/kal_harmonyos/kal_harmonyos.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "kal.timer"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct {
    plum_void (*func)(plum_void *arg);
    plum_u32 *arg;
} kal_tiemr_cb_t;

typedef struct {
    plum_s8               name[PLUM_KAL_TIMER_NAME_MAX];
    hi_u32                hdl;
    plum_u32              period;
    plum_kal_timer_mode_t mode;
    kal_tiemr_cb_t        cb;
    struct list_head      list;
} kal_timer_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

PLUM_PRIVATE kal_timer_t timer_head;

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_s32 kal_timer_add(kal_timer_t *node)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!node) {
            LOG_E("kal_timer_add param err");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        list_add(&node->list, &timer_head.list);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
kal_timer_t *kal_timer_find(PLUM_CONST plum_s8 *name)
{
    struct list_head *pos;
    list_for_each(pos, &timer_head.list)
    {
        kal_timer_t *node = list_entry(pos, kal_timer_t, list);
        if (strcmp((const char *)name, (const char *)node->name) == 0) {
            return (node);
        }
    }

    return (plum_null);
}

PLUM_PRIVATE
kal_timer_t *kal_timer_find_by_hdl(PLUM_CONST plum_s32 hdl)
{
    struct list_head *pos;
    list_for_each(pos, &timer_head.list)
    {
        kal_timer_t *node = list_entry(pos, kal_timer_t, list);
        if (node->hdl == hdl) {
            return (node);
        }
    }

    return (plum_null);
}

PLUM_PRIVATE
plum_s32 kal_timer_del(kal_timer_t *node)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!node) {
            rc = PLUM_ECODE_INVALID;
            break;
        }

        list_del(&node->list);

        plum_kal_mem_free(node);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
kal_timer_t *kal_timer_apply(PLUM_CONST plum_s8 *name, hi_u32 id,
                             kal_tiemr_cb_t *cb, plum_u32 period,
                             plum_kal_timer_mode_t mode)
{
    kal_timer_t *node = plum_null;
    do {
        if (!timer_head.list.next) {
            INIT_LIST_HEAD(&timer_head.list);
        }

        node = kal_timer_find(name);
        if (!node) {
            node = (kal_timer_t *)plum_kal_mem_malloc(sizeof(kal_timer_t));
            if (!node) {
                LOG_E("kal_timer_apply malloc err");
                break;
            }

            snprintf((char *)node->name, sizeof(node->name), "%s", name);
            node->hdl     = id;
            node->cb.func = cb->func;
            node->cb.arg  = cb->arg;
            node->period  = period;
            node->mode    = mode;
        }
    } while (0);

    return (node);
}

PLUM_PRIVATE
plum_void kal_timer_main(plum_u32 timerid)
{
    kal_timer_t *node = kal_timer_find_by_hdl(timerid);
    if (!node) {
        LOG_E("node find the timer,id:%d", timerid);
    }
    else {
        node->cb.func(node->cb.arg);
    }
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_kal_timer_create(PLUM_CONST plum_s8 *name,
                               plum_void (*fn)(plum_void *arg), plum_void *arg,
                               plum_u32 period, plum_kal_timer_mode_t mode)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("timer name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        if (strlen((const char *)name) > PLUM_KAL_MQ_NAME_MAX) {
            LOG_E("The timer name is too long");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        hi_u32 timerid;
        rc = hi_timer_create(&timerid);
        if (rc) {
            LOG_E("hi_timer_create err,rc:0x%08X", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }

        kal_tiemr_cb_t cb;
        cb.func           = fn;
        cb.arg            = arg;
        kal_timer_t *node = kal_timer_apply(name, timerid, &cb, period, mode);
        if (!node) {
            LOG_E("kal_timer_apply err");
            rc = PLUM_ECODE_EMEM;
            break;
        }

        rc = kal_timer_add(node);
        if (rc) {
            LOG_E("kal_timer_add err,rc:%d", rc);
            break;
        }

        rc = hi_timer_start(timerid, (hi_timer_type)mode, period,
                            kal_timer_main, node->hdl);
        if (rc) {
            LOG_E("hi_timer_start err,rc:0x%08X", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_timer_delete(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("timer name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_timer_t *node = kal_timer_find(name);
        if (!node) {
            LOG_E("not find the tiemr,named:%s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = hi_timer_delete(node->hdl);
        if (rc) {
            LOG_E("hi_timer_delete err,rc:0x%08X", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }

        rc = kal_timer_del(node);
        if (rc) {
            LOG_E("kal_timer_del err,rc:%d", rc);
            break;
        }

        LOG_I("delete timer,named:%s", name);
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_u32 plum_kal_timer_suspend(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("timer name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_timer_t *node = kal_timer_find(name);
        if (!node) {
            LOG_E("not find the tiemr,named:%s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = hi_timer_stop(node->hdl);
        if (rc) {
            LOG_E("hi_timer_stop err,rc:0x%08X", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_timer_resume(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("timer name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_timer_t *node = kal_timer_find(name);
        if (!node) {
            LOG_E("not find the tiemr,named:%s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = hi_timer_start(node->hdl, node->mode, node->period, kal_timer_main,
                            node->hdl);
        if (rc) {
            LOG_E("timer_settime err,rc:%d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_void plum_kal_timer_show(PLUM_CONST plum_s8 *name)
{
    if (name) {
        kal_timer_t *node = kal_timer_find(name);
        if (!node) {
            LOG_E("not find the timer,named:%s", name);
            return;
        }
        LOG_I("timer[%d] named : %s", node->hdl, node->name);
    }
    else {
        struct list_head *pos;
        list_for_each(pos, &timer_head.list)
        {
            kal_timer_t *node = list_entry(pos, kal_timer_t, list);
            LOG_I("timer[%d] named : %s", node->hdl, node->name);
        }
    }
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
